package com.example.helloworldspringboot.api;

import com.example.helloworldspringboot.entity.Category;
import com.example.helloworldspringboot.entity.Goods;
import com.example.helloworldspringboot.service.CategoryService;
import com.example.helloworldspringboot.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class GoodsApi {
//    这里对操作返回结果进行封装

    @Autowired
    GoodsService goodsService;
    @Autowired
    CategoryService categoryService;

    @RequestMapping("/goods/addGoods")
    public int addGoods(@RequestParam String name,@RequestParam String price,@RequestParam String quantity,@RequestParam String category,@RequestParam String supplier){
        Goods goods = new Goods();
        goods.setName(name);
        goods.setQuantity(Long.parseLong(quantity));
        goods.setPrice(Float.parseFloat(price));
        goods.setCategory(category);
        goods.setSupplier(supplier);
        goods.setStatus("0");
        return goodsService.addGoods(goods);
    }

    @RequestMapping("/goods/deleteGoods")
    public int deleteGoods(@RequestParam String id){
        Goods goods = new Goods();
        goods.setId(id);
        return goodsService.deleteGoods(goods);
    }

    @RequestMapping("/category/CategoryNameList")
    public HashMap<Integer,String> findAll(){
        List<Category> allCategory =  categoryService.queryAllCategory();
        HashMap<Integer,String> categoryNames = new HashMap<>();
       for (int i=0;i<allCategory.size();i++){
           categoryNames.put(i,allCategory.get(i).getName());
       }
        return categoryNames;
    }
}
